//
//  UIButton+Extension.swift
//  Weibo
//
//  Created by 梁智 on 15/11/22.
//  Copyright © 2015年 lzfree. All rights reserved.
//

import UIKit

extension UIButton {
    
    /// 便利构造函数
    ///
    /// - parameter imageName:     图片名称
    /// - parameter backImageName: 背景图片名称
    ///
    /// - returns:  UIButton
    convenience init(imageName: String, backImageName: String?) {
        
        self.init()
        
        setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        setImage(UIImage(named: imageName + "_highlighted"), forState: UIControlState.Highlighted)
        
        if let backImageName = backImageName {
            
            setBackgroundImage(UIImage(named: backImageName), forState: UIControlState.Normal)
            setBackgroundImage(UIImage(named: backImageName + "_highlighted"), forState: UIControlState.Highlighted)
        }
        
        sizeToFit()
    }
    
    /// 便利构造函数
    ///
    /// - parameter title:         文字
    /// - parameter titleColor:    文字颜色
    /// - parameter backImageName: 背景图片名称
    ///
    /// - returns: UIButton
    convenience init(title: String, titleColor: UIColor, backImageName: String) {
        self.init()
        
        setTitle(title, forState: .Normal)
        setTitleColor(titleColor, forState: .Normal)
        setBackgroundImage(UIImage(named: backImageName), forState: .Normal)
        
        sizeToFit()
        
    }
    
    /// 便利构造函数
    ///
    /// - parameter title:      文字
    /// - parameter imageName:  图片名称
    /// - parameter fontSize:   文字大小
    /// - parameter titleColor: 文字颜色
    /// - parameter backColor:  背景颜色
    ///
    /// - returns:  UIButton
    convenience init(title: String, imageName: String?, fontSize: CGFloat = 12, titleColor: UIColor = UIColor.lightGrayColor(), backColor: UIColor? = nil) {
        self.init()
        
        setTitle(title, forState: .Normal)
        setTitleColor(titleColor, forState: .Normal)
        titleLabel?.font = UIFont.systemFontOfSize(fontSize)
        
        if let imageName = imageName {
            setImage(UIImage(named: imageName), forState: .Normal)
        }
        
        backgroundColor = backColor
        
        sizeToFit()
    }
}
